<?php

/**
 * 专辑管理 -- 推荐功能提交程序
 * 
 * @copyright caijian
 * @since 2012-03-20
 */


include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	if(! security_check::check_refer())
	{
		throw new Exception("来路不明");
	}
	
	// id
	cgi::both($g_cgival["album_id"], "album_id", "");
	cgi::both($g_cgival["old_id"], "old_id", 0);
	cgi::both($g_cgival["pos"], "pos", 0);
	cgi::both($g_cgival["site_id"], "site_id", 0);
	cgi::both($g_cgival["id"], "id", "");

	// op
	cgi::both($g_cgival["op"], "op", "");

	// position
	cgi::both($g_cgival["positions"], "positions", "");


	
}

function get_data()
{
	global $config, $g_cgival, $g_pro, $g_show;

	$album_id = intval($g_cgival["album_id"]);
	$pos = intval($g_cgival["pos"]);

	$op = $g_cgival["op"];

	if (! $op )
	{
		throw new Exception("参数错误！");
	}

	if (! $album_id && empty($g_cgival['id']))
	{
		throw new Exception("参数错误:缺少album id！");
	}


	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();
	$admin_id = admin::admin_get_login_uid();
	$admin_name = admin::admin_get_login_user_name();

//  	$db->debug = true;
	
	$db_album_tj = new db_food_album_tuijian();
	$db_album = new db_food_album();
	$db_daren = new db_food_user_daren();
	$fuser = new mod_food_user();
	$admin_log = new admin_log();

	// 判断
	switch ($op) {

	case "add":

		if (! $g_cgival['positions'] )
		{
			throw new Exception("参数错误:缺少推荐位！");
		}

		$checkArr[] = $album_id;
		$tmp_data = $db_album->admin_get_food_album_by_ids($checkArr);
		if (! is_array($tmp_data[0]) ) {
			throw new Exception('专辑id不存在');
		}
		if ($tmp_data[0]["admin_check_status"]!=1)
		{
			throw new Exception('专辑未审核通过');
		}
		if ($tmp_data[0]["album_status_count"]<10)
		{
			throw new Exception('专辑收录数不足10条');
		}
		$album_data = $tmp_data[0];

		$uid = $album_data['create_uid'];

		$userdata = $fuser->get_food_user_info($uid);

		if ($userdata['error']) {
			$g_show['error'] = 1;
			$g_show['errmsg'] = "专辑对应uid: $uid 用户不存在";
			return;
		}

		// 添加到推荐
		$g_pro['positions'] = explode(',',$g_cgival['positions']);

		foreach ($g_pro['positions'] as $album_tj_pos) {
			if ($album_tj_pos > 0) {

				// 检查是否存在：
				$tj = $db_album_tj->admin_get_album_tj_by_album_pos($site_id,$album_id,$album_tj_pos);

				if ($tj) {

					if (! $tj['status']) {
						$db_album_tj->admin_update_album_tj_status_by_id($site_id,$album_id,$album_tj_pos,1);
					}else {						
						$g_pro['error'][] = $config['food_album_tj_pos'][$album_tj_pos];
					}

				} else {

					// 调整顺序，将已有专辑位置都+1：
					$db_album_tj->admin_increase_album_tj_seq($site_id,$album_tj_pos);

					// 添加推荐
					$arr = array('site_id'	=>	$site_id
						,'album_id'	=>	$album_id
						,'album_tj_pos'	=>	$album_tj_pos
						,'album_pic'	=>	$album_data['album_big_pic']
						,'album_small_pic'	=>	$album_data['album_small_pic1']
						,'album_title'	=>	$album_data['album_title']
						,'album_content'	=>	$album_data['album_content']
						,'nickname'	=>	$userdata['nickname']
						,'admin_id'	=>	$admin_id
						,'admin_name'	=>	$admin_name
						,'uid' => $uid);
					$db_album_tj->admin_insert_album_tuijian($arr);

					// 添加专辑作者至 达人表
					$db_daren->admin_insert_food_user_daren($uid, $site_id, $userdata['nickname'], 1, 1,$admin_id,$admin_name);

					// 如果推荐专辑数量超出指定数量，修改多余的专辑状态为0：
					//$db_album_tj->admin_update_exceed_album_tj_status($site_id,$album_tj_pos,$config['food_album_total_tj_num'][$album_tj_pos]);
				}

			}
		}
		if(!empty($g_pro['error']))
		{
			$g_show['error'] = 1;
			$g_show['errmsg'] = "以下专辑已被推荐:\r\n".implode("\r\n",$g_pro['error']);
		}

		$admin_log->admin_insert_log_info($admin_id, $site_id, 11, "新增 专辑推荐album_id={$album_id}");
		break;
	case "edit":
		
		$checkArr[] = $album_id;
		$tmp_data = $db_album->admin_get_food_album_by_ids($checkArr);
		if (! is_array($tmp_data[0]) ) {
			throw new Exception('专辑id不存在');
		}
		$album_data = $tmp_data[0];
		$uid = $album_data['create_uid'];
		$userdata = $fuser->get_food_user_info($uid);

		if ($userdata['error']) {
			$g_show['error'] = 1;
			$g_show['errmsg'] = "专辑对应uid: $uid 用户不存在";
			return;
		}


		$album_tj_pos = intval($g_cgival['pos']);
		if (! $album_tj_pos) {
			throw new Exception('参数错误：缺少推荐位');
		}

		// 读取原推荐数据：
		$old_id = intval($g_cgival["old_id"]);
		$oldtj = $db_album_tj->admin_get_album_tj_by_album_pos($site_id,$old_id,$album_tj_pos);

		if (! $oldtj) {
			throw new Exception('原推荐专辑不存在');
		}

		// 检查是否已经推荐过：
		$tj = $db_album_tj->admin_get_album_tj_by_album_pos($site_id,$album_id,$album_tj_pos);

		if ($tj) {

			if (! $tj['status']) {
				// 调整顺序，将已有专辑位置都+1：
				$db_album_tj->admin_increase_album_tj_seq($site_id,$album_tj_pos);
				$db_album_tj->admin_update_album_tj_status_by_id($site_id,$album_id,$album_tj_pos,1);
				$db_album_tj->admin_update_album_tj_seq($album_data[0]['id'],1);

			} else {
				throw new Exception('已经添加过这个专辑');
			}

		} else {

			// 更新原推荐专辑
			$arr = array('site_id'	=>	$site_id
				,'album_id'	=>	$album_id
				,'album_tj_pos'	=>	$album_tj_pos
				,'album_pic'	=>	$album_data['album_big_pic']
				,'album_small_pic'	=>	$album_data['album_small_pic1']
				,'album_title'	=>	$album_data['album_title']
				,'album_content'	=>	$album_data['album_content']
				,'nickname'	=>	$userdata['nickname']
				,'admin_id'	=>	$admin_id
				,'admin_name'	=>	$admin_name
				,'uid' => $uid);
			$db_album_tj->admin_update_album_tj_by_id($oldtj['id'],$arr);

			// 添加专辑作者至 达人表
			$db_daren->admin_insert_food_user_daren($uid, $site_id, $userdata['nickname'], 1, 1,$admin_id,$admin_name);

			// 如果推荐专辑数量超出指定数量，修改多余的专辑状态为0：
			//$db_album_tj->admin_update_exceed_album_tj_status($site_id,$album_tj_pos,$config['food_album_total_tj_num'][$album_tj_pos]);
		}

		$admin_log->admin_insert_log_info($admin_id, $site_id, 11, "编辑 专辑推荐album_id={$album_id}");
		break;
		
	case "del":

		if ($site_id != 900 && $site_id != $g_cgival['site_id']) {
			throw new Exception("站点权限错误");
		}

		$db_album_tj->admin_update_album_tj_status_by_id($site_id,$album_id,$pos,0);
		$admin_log->admin_insert_log_info($admin_id, $site_id, 11, "删除 专辑推荐album_id={$album_id}");
		break;
	case "up":
		$albumdata = $db_album_tj->admin_get_album_tj($site_id,$album_id,$pos);
		$albumnext = $db_album_tj->admin_get_album_tj_next($albumdata[0]['album_seq'],'up');

		if ( $albumnext) {
			$db_album_tj->admin_update_album_tj_seq($albumdata[0]['id'],$albumnext[0]['album_seq']);
			$db_album_tj->admin_update_album_tj_seq($albumnext[0]['id'],$albumdata[0]['album_seq']);
		}
		
		$admin_log->admin_insert_log_info($admin_id, $site_id, 11, "上移 专辑推荐album_id={$album_id}");
		break;

	case "down":
		$albumdata = $db_album_tj->admin_get_album_tj($site_id,$album_id,$pos);
		$albumnext = $db_album_tj->admin_get_album_tj_next($albumdata[0]['album_seq'],'down');

		if ( $albumnext) {
			$db_album_tj->admin_update_album_tj_seq($albumdata[0]['id'],$albumnext[0]['album_seq']);
			$db_album_tj->admin_update_album_tj_seq($albumnext[0]['id'],$albumdata[0]['album_seq']);
		}
		
		$admin_log->admin_insert_log_info($admin_id, $site_id, 11, "下移 专辑推荐album_id={$album_id}");
		break;
	//add 全部删除 by renxin1 6/5
	case "delall";
		
		$reset = $db_album_tj->admin_delete_album_tj_by_ids($g_cgival['id']);
		$admin_log->admin_insert_log_info($admin_id, $site_id, 11, "删除 专辑推荐album_idin({$album_id})");
		break;
	default:
		$g_show['error'] = 1;
		$g_show['errmsg'] = '操作未定义';
	}

}

try
{

	admin_check_login();
// 	admin_check_user_priv("foodv2/main_nav.php");
	
	check_cgi_pro();
	get_data();
	
	if (! $g_show['error']) {
		$g_show["error"] = 0;
		$g_show["errmsg"] = "成功";	
	}

}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_show;
	
	echo json_encode($g_show);
}

?>